//
//  YYTJ2ViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/15.
//  Copyright © 2016年 sgone. All rights reserved.
//

#import "YYTJ2ViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
//添加pickView
#import "HZQDatePickerView.h"
#import "YYTJ2Cell.h"
@interface YYTJ2ViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,HZQDatePickerViewDelegate>


{
    
    UIButton *btnRL;
    UILabel *lblJE1;
    UILabel *lblDD1;
    UILabel *lblJE2;
    UILabel *lblYF;
    UILabel *lblDD2;
    UILabel *lblWC;
    UILabel *lblDS;
    
    NSString *date1;
    NSMutableDictionary *list;
    UITextField *riliTextField;
    
    
    
   

    
 
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong)  YYTJ2Cell *cell2;
@property(nonatomic,strong) HZQDatePickerView *_pikerView;


@end

@implementation YYTJ2ViewController


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = BACKGROUND1;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.titleLabel.text = @"营业统计";
    self.backBtn.hidden = NO;
    
    //获得当前的时间
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    date1 = [formatter stringFromDate:[NSDate date]];
    
    [self creatData];
    
    
}


-(void)creatData
{
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSDictionary *parmas = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"report_time" : date1};
    
    NSString *urlStr = [NSString stringWithFormat:@"%@get_business_report",SERVER_ADDRESS2];

    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject)
     {
     
     if ([responseObject[@"error_flag"]intValue] == 0)
     {
     
         
         NSDictionary *dict = responseObject[@"data"][@"list"];
         list = [NSMutableDictionary dictionaryWithDictionary:dict];
         [MBProgressHUD hideHUDForView:self.tableView animated:YES];
         [self.tableView reloadData];

     }
     else
     {
     
        NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
         [self.view makeToast:str];
         [MBProgressHUD hideHUDForView:self.tableView animated:YES];
     
     
     }
     }
     failure:^(NSURLSessionDataTask *task, NSError *error) {
         
         [self.view makeToast:@"请检查网络!"];
         [MBProgressHUD hideHUDForView:self.tableView animated:YES];
 
         
         
     }];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;


}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    if (section == 0) {
        
        return 44 / FDLaOt;
    }
    
    else if (section == 2)
    {
    
        return 40 / FDLaOt;
    }
    else if (section == 1)
    {
        return 0.01;
    
    }
    else
    {
    
        return 0;
    }

}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    if (section == 0) {
        
      return  [self contentView1];
    }
  
     else if (section == 2)
    {
    
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 10 / FDLaOt)];
        view2.backgroundColor = [UIColor whiteColor];
        
        UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 10 /FDLaOt)];
        view3.backgroundColor = BACKGROUND1;
        [view2 addSubview:view3];
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 /FDLaOt, ViewWidth, 30 /FDLaOt)];
        lbl.font = [UIFont systemFontOfSize:15/FDLaOt];
        lbl.text = @"  订单统计";
        [view2 addSubview:lbl];
        
        return view2;
    
    }
    else
    {
        return nil;
    }

}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section == 0  || section == 1 ) {
        
        return 0.001;
    }
    
    else
    {
        
        return 200 / FDLaOt;
        
    }
    
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{


    
    if (section == 0 && section == 1) {
        
        
        return nil;
    }
    
    else
    {
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    view2.backgroundColor = BACKGROUND1;
    
    
    
    return view2;
    }
}





-(UIView *)contentView1
{
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 10)];
    view3.backgroundColor = [UIColor whiteColor];
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth / 3 - 30 /FDLaOt, 0, 80 / FDLaOt, 44/FDLaOt)];
    lbl1.text = @"选择日期:";
    lbl1.font = [UIFont systemFontOfSize:14 / FDLaOt];
    lbl1.textColor = [UIColor grayColor];
    [view3 addSubview:lbl1];
    
    
    
    riliTextField = [[UITextField alloc]initWithFrame:CGRectMake(lbl1.frame.origin.x + lbl1.frame.size.width, 1/ FDLaOt, 130/FDLaOt, 43 /FDLaOt)];
    riliTextField.textColor = [UIColor grayColor];
    [view3 addSubview:riliTextField];
    riliTextField.font = [UIFont systemFontOfSize:14 / FDLaOt];
    riliTextField.backgroundColor = [UIColor whiteColor];
    riliTextField.enabled = NO;
    riliTextField.placeholder = @"点击获取时间";
    riliTextField.textAlignment = NSTextAlignmentLeft;
    riliTextField.text = date1;
    
    riliTextField.rightViewMode = UITextFieldViewModeAlways;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 9 /FDLaOt, 17/ FDLaOt, 22 /FDLaOt)];
    imageView.image = [UIImage imageNamed:@"icon_date2"];
    imageView.backgroundColor = [UIColor whiteColor];
    riliTextField.rightView = imageView;
    
    
    UILabel* lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(0, 44 /FDLaOt, ViewWidth, 0.2 )];
    lbl4.backgroundColor = [UIColor lightGrayColor];
    [view3 addSubview:lbl4];
    
    
    //添加button弹出pickView，显示日期操作
    UIButton *pickBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ViewWidth , 44 / FDLaOt)];
    [view3 addSubview:pickBtn];
    [pickBtn addTarget:self action:@selector(pickAdd:) forControlEvents:UIControlEventTouchUpInside];
    
    return view3;
   

  

}



#pragma mark pick弹出操作

-(void)pickAdd:(id)sender
{
    
    [self setupDateView:DateTypeOfStart];
    
    
}



- (void)setupDateView:(DateType)type {
    
    self._pikerView = [HZQDatePickerView instanceDatePickerView];
    self._pikerView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight + 20);
    [self._pikerView setBackgroundColor:[UIColor clearColor]];
    self._pikerView.delegate = self;
    self._pikerView.type = type;
    //    [_pikerView.datePickerView setMinimumDate:[NSDate date]];
    [self.view addSubview:self._pikerView];
    
}

- (void)getSelectDate:(NSString *)date type:(DateType)type {
       NSLog(@"%d - %@", type, date);
    
    riliTextField.text = @"";
    date1 = date;
    riliTextField.text = date1;
    
    
    
    
    
    switch (type) {
        case DateTypeOfStart:
            
        {
        
            [self creatData];
         
            
//刷新指定的section
//                    NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
//                    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
   
            
        }
            break;
            
            
            
        default:
            break;
    }
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    return 70 / FDLaOt;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
  
    

    self.cell2 = [YYTJ2Cell  cellWithTableView:tableView];
    self.cell2.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {
        
        self.cell2.backgroundColor = BACKGROUND1;
       
        for (int i = 0; i < 2; i++) {
            UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(i * ViewWidth /2, 4 / FDLaOt, ViewWidth/2, 35/ FDLaOt)];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.font = [UIFont systemFontOfSize:14/FDLaOt];
            
            
            
            UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(i * ViewWidth /2, 30 / FDLaOt, ViewWidth/2, 35/ FDLaOt)];
            lbl2.textAlignment = NSTextAlignmentCenter;
            [lbl2 setNumberOfLines:0];
            lbl2.font = [UIFont systemFontOfSize:14/FDLaOt];
            lbl2.textColor= BenSe_COLOR;
            
            if (i == 0) {
                
                lbl.text = @"成交总金额(元)";
                lbl2.text = [NSString stringWithFormat:@"%@",list[@"amount"]];
    
        
            }
            else if (i == 1)
            {
                lbl.text = @"成交总订单(单)";
                lbl2.text = [NSString stringWithFormat:@"%@",list[@"count"]];

            
            }
            
             [self.cell2 addSubview:lbl];
             [self.cell2 addSubview:lbl2];
        }
        
        
    }
    else if (indexPath.section == 1)
    {
        
        UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth / 2, 15 / FDLaOt, 1, 40 / FDLaOt)];
        lbl1.backgroundColor = BACKGROUND1;
        [self.cell2 addSubview:lbl1];
        
        
        for (int i = 0; i < 2; i++) {
            
            UILabel *lbl2 =[[UILabel alloc]initWithFrame:CGRectMake(i * ViewWidth /2, 4/FDLaOt, ViewWidth / 2, 35)];
            lbl2.textColor = [UIColor grayColor];
            lbl2.textAlignment = NSTextAlignmentCenter;

            lbl2.font = [UIFont systemFontOfSize:13/FDLaOt];
            [self.cell2 addSubview:lbl2];
            
            
            
            
            UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(i * ViewWidth /2, 30 / FDLaOt, ViewWidth/2, 35/ FDLaOt)];
            lbl3.textAlignment = NSTextAlignmentCenter;
            [lbl3 setNumberOfLines:0];
            lbl3.font = [UIFont systemFontOfSize:13/FDLaOt];
            lbl3.textColor= [UIColor grayColor];
            [self.cell2 addSubview:lbl3];
            [lbl3 setNumberOfLines:0];
            
            if (i == 0) {
                
                lbl2.text = @"货款金额";
                lbl3.text = [NSString stringWithFormat:@"%@元",list[@"product_amount"]];
            }
            else if (i == 1)
            {
             lbl2.text = @"运费金额";
             lbl3.text = [NSString stringWithFormat:@"%@元",list[@"trans_amount"]];
            
            }
            
            
            
            
         

            
        }
       
         
         
         
    
    }
    else
    {
        self.cell2.backgroundColor = [UIColor whiteColor];
        UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ViewWidth /2 /2, 35/FDLaOt)];
        lbl1.text= @"   全部订单:";
        lbl1.textColor = [UIColor grayColor];
        [self.cell2 addSubview:lbl1];
        lbl1.font = [UIFont systemFontOfSize:13 /FDLaOt];
        
        
        UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(lbl1.frame.origin.x + lbl1.frame.size.width, 0, ViewWidth /2 /2, 35/FDLaOt)];
        lbl2.text = [NSString stringWithFormat:@"%@单",list[@"order_report"][@"all"]];
        lbl2.textAlignment = NSTextAlignmentLeft;
        lbl2.font = [UIFont systemFontOfSize:13 /FDLaOt];
        lbl2.textColor = BenSe_COLOR;
        lbl2.numberOfLines = 0;
        [self.cell2 addSubview:lbl2];
        
        
        
        UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth /2 + 20, 0, ViewWidth /2 /2 -20, 35/FDLaOt)];
        lbl11.text= @"   已完成:";
        lbl11.textColor = [UIColor grayColor];
        [self.cell2 addSubview:lbl11];
        lbl11.font = [UIFont systemFontOfSize:13 /FDLaOt];
        
        
        
        UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(lbl11.frame.origin.x + lbl11.frame.size.width, 0, ViewWidth /2 /2, 35/FDLaOt)];
        lbl3.text = [NSString stringWithFormat:@"%@单",list[@"order_report"][@"complete"]];
        lbl3.textAlignment = NSTextAlignmentLeft;
        lbl3.font = [UIFont systemFontOfSize:13 /FDLaOt];
        lbl3.textColor = BenSe_COLOR;
        lbl3.numberOfLines = 0;
        [self.cell2 addSubview:lbl3];
    
        
        
        
        
        UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(0 , 35/FDLaOt - 4 / FDLaOt, ViewWidth /2 /2 , 35/FDLaOt)];
        lbl22.text= @"       待送货:";
        lbl22.textColor = [UIColor grayColor];
        [self.cell2 addSubview:lbl22];
        lbl22.font = [UIFont systemFontOfSize:13 /FDLaOt];
        
        
        UILabel *lbl23 = [[UILabel alloc]initWithFrame:CGRectMake(lbl22.frame.origin.x + lbl22.frame.size.width, 35/FDLaOt - 4 / FDLaOt, ViewWidth /2 /2, 35/FDLaOt)];
        lbl23.text = [NSString stringWithFormat:@"%@单",list[@"order_report"][@"wait_send"]];
        lbl23.textAlignment = NSTextAlignmentLeft;
        lbl23.font = [UIFont systemFontOfSize:13 /FDLaOt];
        lbl23.textColor = BenSe_COLOR;
        lbl23.numberOfLines = 0;
        [self.cell2 addSubview:lbl23];
    }

    
    
    [self.cell2 loadCell];
    
 
    return self.cell2;


}




@end
